package com.lhy.admin.common.mvcplus.wrapper.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lhy.admin.common.mvcplus.entity.ComparatorEnum;
import com.lhy.admin.common.mvcplus.entity.DbField;
import com.lhy.admin.common.mvcplus.wrapper.AbstractSearchComparator;

/**
 * FindInSetComparator
 *
 * @author whx
 * @date 2022/12/21 18:24
 */
public class FindInSetComparator extends AbstractSearchComparator {

    public static final String FIND_IN_SET_SQL_TEMP = "FIND_IN_SET({0}, %s)";

    @Override
    public void accept(QueryWrapper queryWrapper, DbField dbField) {
        queryWrapper.and(qw -> {
            QueryWrapper qw1 = (QueryWrapper) qw;
            String[] vals = splitStr(dbField.getValue());
            for (int i = 0; i < vals.length; i++) {
                qw1.apply(sql(dbField.getName()), vals[i]);
                if (i < vals.length - 1) {
                    qw1.or();
                }
            }
        });
    }
    public static String sql(String colName) {
        return FIND_IN_SET_SQL_TEMP.formatted(colName);
    }
    @Override
    public ComparatorEnum comparatorEnum() {
        return ComparatorEnum.FIND_IN_SET;
    }
}
